package com.cykj.faceledtv.utils;


import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

/**
 * 获取mac地址
 *
 * @author jzh
 * @date 2018/11/05 11:22
 */

public class MacUtils {
    public static String getMac() {
        try {
            Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
            while (interfaces.hasMoreElements()) {
                NetworkInterface nif = interfaces.nextElement();
                byte[] addr = nif.getHardwareAddress();
                if (addr == null || addr.length <= 0) {
                    continue;
                }
                StringBuilder sb = new StringBuilder();
                for (byte b : addr) {
                    sb.append(String.format("%02X:", b));
                }
                if (sb.length() > 0) {
                    sb.deleteCharAt(sb.length() - 1);
                }
                return sb.toString();
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return "02:00:00:00:00";
    }

    public static byte[] getMacToByte() {
        try {
            Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
            while (interfaces.hasMoreElements()) {
                NetworkInterface nif = interfaces.nextElement();
                byte[] addr = nif.getHardwareAddress();
                if (addr == null || addr.length <= 0) {
                    continue;
                }
                return addr;
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return new byte[]{0, 0, 0, 0, 0, 0};
    }
}
